﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TFSTools.TFSLibrary.TestManagement;

namespace TFSTools.TestManagement.TestCaseReview
{
    public class TestCaseSorter : IComparable
    {
        private readonly TFSTestCase testCase;
        private readonly string testCaseComparer;

        public TestCaseSorter(TFSTestCase testCase)
        {
            this.testCase = testCase;
            this.testCaseComparer = testCase.Title;
        }

        public TFSTestCase TestCase
        {
            get { return this.testCase; }
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            TestCaseSorter other = obj as TestCaseSorter;
            if (other == null)
            {
                throw new Exception("The method or operation is not implemented.");
            }
            else
            {
                return string.Compare(this.testCaseComparer, other.testCaseComparer);
            }
        }

        #endregion

        public static List<TFSTestCase> Sort(TFSTestCaseCollection list)
        {
            var sorterList = new List<TestCaseSorter>(list.Count);
            foreach (var testCase in list)
            {
                sorterList.Add(new TestCaseSorter(testCase));
            }

            sorterList.Sort();

            var sortedList = new List<TFSTestCase>(list.Count);
            sorterList.ForEach(testCase => sortedList.Add(testCase.TestCase));
            return sortedList;
        }
    }
}
